<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'bas:oper:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasFyBasOperForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { TestTree, operForm, operSave, operTreeData } from '/@/api/test/testTree';
  import { officeTreeData } from '/@/api/sys/office';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('bas.oper');
  const { showMessage } = useMessage();
  const record = ref<TestTree>({} as TestTree);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增数据') : t('编辑数据'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('上级工序'),
      field: 'parentCode', // parent.id
      fieldLabel: 'parentName', // parent.operName
      component: 'TreeSelect',
      componentProps: {
        allowClear: true,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('工序编号'),
      field: 'operCode',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
      rules: [
        { required: true },
        { pattern: /^[a-zA-Z0-9_]*$/, message: t('请输入字母数字下划线') },
      ],
    },
    {
      label: t('工序名称'),
      field: 'operName',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      required: true,
    },
    {
      label: t('是否委外'),
      field: 'bout',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('是否检验'),
      field: 'bcheck',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('是否数铣'),
      field: 'bsx',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('是否入库'),
      field: 'brk',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('允许补料'),
      field: 'canbl',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('允许完工'),
      field: 'canOver',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('合格率统计'),
      field: 'brate',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('上传附件'),
      field: 'bfile',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },
    {
      label: t('上传图片'),
      field: 'bpic',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_menu_type',
        options: [{ label: '', value: '1' }],
      },
      colProps: { lg: 8, md: 24 },
    },

    {
      label: t('时长(正常)'),
      field: 'durtimeNor',
      helpMessage: '分钟',
      component: 'InputNumber',
      defaultValue: 0,
      componentProps: {
        maxlength: 19,
      },
    },
    {
      label: t('时长(加急)'),
      field: 'durtimeUrgent',
      helpMessage: '分钟',
      component: 'InputNumber',
      defaultValue: 0,
      componentProps: {
        maxlength: 19,
      },
    },
    {
      label: t('时长(特急)'),
      field: 'durtimeExurgent',
      helpMessage: '分钟',
      component: 'InputNumber',
      defaultValue: 0,
      componentProps: {
        maxlength: 19,
      },
    },
    {
      label: t('显示顺序'),
      field: 'treeSort',
      // helpMessage: '升序',
      component: 'InputNumber',
      componentProps: {
        maxlength: 10,
      },
      // defaultValue: '30',
      required: true,
    },
    {
      label: t('工序负责人'),
      field: 'manCodes',
      fieldLabel: 'manNames',
      component: 'TreeSelect',
      componentProps: {
        api:officeTreeData,
        params: { isLoadUser: true, userIdPrefix: '',isAll:true },
        treeCheckable: true,
        allowClear: true,
        // canSelectParent: false,
      },
      colProps: { lg: 24, md: 24 },
    },
    // {
    //   label: t('上级数据'),
    //   field: 'parentCode',
    //   fieldLabel: 'parentName',
    //   component: 'TreeSelect',
    //   componentProps: {
    //     allowClear: true,
    //   },
    // },
    // {
    //   label: t('节点编码'),
    //   field: 'treeCode',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 64,
    //   },
    //   rules: [
    //     { required: true },
    //     { pattern: /^[a-zA-Z0-9_]*$/, message: t('请输入字母数字下划线') },
    //   ],
    // },
    // {
    //   label: t('排序号'),
    //   field: 'treeSort',
    //   helpMessage: '升序',
    //   component: 'InputNumber',
    //   componentProps: {
    //     maxlength: 10,
    //   },
    //   defaultValue: '30',
    //   required: true,
    // },
    // {
    //   label: t('节点名称'),
    //   field: 'treeName',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 200,
    //   },
    //   required: true,
    // },
    // {
    //   label: t('备注信息'),
    //   field: 'remarks',
    //   component: 'InputTextArea',
    //   componentProps: {
    //     maxlength: 500,
    //   },
    //   colProps: { lg: 24, md: 24 },
    // },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await operForm(data);
    record.value = (res.basOper || {}) as TestTree;
    if (data.parentCode && data.parentName) {
      record.value.parentCode = data.parentCode;
      record.value.parentName = data.parentName;
    }
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'parentCode',
        componentProps: {
          api: operTreeData,
          params: {
            excludeCode: record.value.id,
            isShowRawName: true,
          },
        },
      },
      {
        field: 'operCode',
        componentProps: {
          disabled: !record.value.isNewRecord,
        },
      },
    ]);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        // treeCode: record.value.treeCode,
        operCode: data.operCode,
      };
      // console.log('submit', params, data, record);
      const res = await operSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
